package com.byxy.ssm.demo.controller;


import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.byxy.ssm.demo.dao.UserDao;
import com.byxy.ssm.demo.entity.Forum;
import com.byxy.ssm.demo.entity.Reply;
import com.byxy.ssm.demo.entity.Tab;
import com.byxy.ssm.demo.entity.Tip;
import com.byxy.ssm.demo.entity.User;
import com.byxy.ssm.demo.service.ForumService;
import com.byxy.ssm.demo.service.TabService;
import com.byxy.ssm.demo.service.TipService;
import com.byxy.ssm.demo.service.ReplyService;
import com.byxy.ssm.demo.vo.TipVO;

import lombok.extern.slf4j.Slf4j;

@Controller
@Slf4j
public class TipController {
	@Autowired
	private ForumService forumService;
	@Autowired
	private TabService tabService;
	@Autowired
	private TipService tipService;
	@Autowired
	private UserDao userDao;
	
	
	@Autowired
	private ReplyService replyService;
	
	@GetMapping("toPublishTipPage.do")//发帖连接
	public String Release(Model model){
		//
		List<Forum> forums = forumService.findAll();
		model.addAttribute("forums",forums);
		//
		List<Tab> tabs = tabService.findAll();
		model.addAttribute("tabs",tabs);
		return "publishTip";
	}
	
	@PostMapping("publishNewTip.do")
	public String doRelease(Tip tip,String selectedForumId,HttpSession session){
		log.info("tipContent="+tip.getTipContent());
		log.info("tipTitle="+tip.getTipTitle());
		log.info("TabId = "+ tip.getTabId());
		log.info("selectedForumId="+selectedForumId);
		User u = (User)session.getAttribute("USER");
		tip.setUserId(u.getUserId());
		tip.setTipClick(0);
		tip.setTipIsdeleted(0);
		tip.setTipIsKnot(0);
		tip.setTipModifyTime(new Date());
		tip.setTipPublishTime(new Date());
		tip.setTipReplies(0);
		tipService.addRelease(tip);
		return "redirect:toMainPage.do";
	}
	
	@GetMapping("showTip.do")//回帖连接
	public String signUp(Integer tipId,Model model){
		log.info("tipId = " + tipId);
		TipVO tipVo = tipService.findById(tipId);
		model.addAttribute("tip", tipVo);
		List<Reply> replies = replyService.findByTip(tipId);
		model.addAttribute("replies", replies);
		return "tipContent";
	}
	
	@PostMapping("publishReply.do")
	public String dosignUp(Integer tipId,String replyContent){
		log.info("tipId="+tipId);
		log.info("replyContent="+replyContent);
		tipService.addContent(tipId,replyContent);
		
		return "redirect:showTip.do";
	}
	
}
